<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.List"%>
<%@page import="Entity.Student"%>
<%@page import="java.util.StringTokenizer"%>
<%@page import="java.io.RandomAccessFile"%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="Assets/css/bootstrap.min.css" />
        <link rel="stylesheet" href="Assets/css/style.css" />
        <title>Review the file which you have just uploaded</title>
    </head>
    <body>
        <%
            String filepath = (String) request.getAttribute("filepath");
            if (filepath != null) {
                session.setAttribute("csvpath",filepath);
                try {
                    List<Student> studentsList = new ArrayList();
                    File f = new File(filepath);
                    if (f.exists()) {
                        RandomAccessFile raf = new RandomAccessFile(filepath, "r");
                        String s = "";
                        raf.readLine();
                        StringTokenizer st;
                        while ((s = raf.readLine()) != null) {
                            st = new StringTokenizer(s, ",");
                            while (st.hasMoreTokens()) {
                                String name = st.nextToken().trim();
                                int age = Integer.parseInt(st.nextToken().trim());
                                int sex = Integer.parseInt(st.nextToken().trim());
                                String hometown = st.nextToken().trim();
                                Student person = new Student(name, age, sex, hometown);
                                studentsList.add(person);
                            }
                        }
                    } else {
                        out.print("This file is not exist!");
                    }
                    if (!studentsList.isEmpty()) {
        %>
        <div class="container">
            <div class="row">
                <h2 class="sub-header">Review the file which you have just uploaded!</h2>
                <div class="row">
                    <div class="pull-right"> 
                    </div>
                </div><div class="row">
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Age</th>
                                    <th>Sex</th>
                                    <th>Hometown</th>
                                </tr>
                            </thead>
                            <tbody>
                                <%
                                    for (Student s : studentsList) {
                                %>
                                <tr>
                                    <td><%=s.getName()%></td>
                                    <td><%=s.getAge()%></td>
                                    <td><%=s.getSex()%></td>
                                    <td><%=s.getHometown()%></td>
                                </tr>      
                                <%
                                    }
                                %>

                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <a href="BulkAdd?comfirm=yes" class="btn btn-warning">Add</a>
                    <a href="BulkAdd?comfirm=no" class="btn btn-danger">Cancel</a>
                </div>

            </div>
        </div>        
        <%
                    }
                } catch (Exception e) {
                    out.print("Something went wrong!");
                }
            }
        %>
    </body>
</html>
